package com.hualan.dao.impl;

import com.hualan.bean.PanghuCommunity;
import com.hualan.dao.CommunityListDao;
import com.hualan.utils.C3p0Util;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 小区列表Dao实现层
 */
public class CommunityListDaoImpl implements CommunityListDao {
    //查询小区总数
    @Override
    public Long CommunityTotal() {
        String sql = "select count(*) as count from panghu_community";
        Map<String, Object> map = C3p0Util.queryOne(sql);
        Object count = map.get("count");
        return (Long) count;
    }

    //查询小区
    @Override
    public List<PanghuCommunity> communityList(String name, String startTime, String endTime, String pageNum, String pageSize, String communityName) {
        //动态sql语句用来可用来条件查询
        String condition = " where 1 = 1";
        //创建列表存储拼接的参数,参数为不定参
        List<Object> paramsList = new ArrayList<>();
        //判断参数是否存在，若存在则拼接 加入列表
//        if (name != null && !"".equals(name)){
//            condition += " and community_name = ?";
//            paramsList.add(name);
//        }
        if (name!=null && !"".equals(name)){
            condition=condition+" and community_name like "+"'%"+name+"%'";
        }
        if (startTime != null && !startTime.equals("")){
            condition += " and create_time >= ?";
            paramsList.add(startTime);
        }
        if (endTime != null && !endTime.equals("")){
            condition += " and create_time <= ?";
            paramsList.add(endTime);
        }
//        if (communityName != null && !"".equals(communityName)){
//            condition += " and community_name = ?";
//            paramsList.add(communityName);
//        }
        if (pageNum.equals("-1")){
            condition += " limit " + pageNum + "," + pageSize;
        }

//        System.out.println(paramsList);
        String sql = "select * from panghu_community" + condition;
//        System.out.println(sql);
        List<PanghuCommunity> list = C3p0Util.query(sql, PanghuCommunity.class, paramsList.toArray(new Object[0]));
//        System.out.println(list);
        return list;
    }
}
